#*****************************************************************************
# Copyright Jack Lee. All rights reserved.
# Licensed under the MIT License.
# Email: 291148484@163.com
# https://blog.csdn.net/qq_28550263?spm=1001.2101.3001.5343
#*****************************************************************************


function Is-Folder([string]$Path) {
    <# 
    返回给定的路径是否是一个目录（文件夹） 
    #>
    return Test-Path -Path $Path -PathType Container
}

function Is-Exist([string]$Path) {
    <# 
    返回文件是否存在
    #>
    return Test-Path -Path $Path
}

function Check-Folder([string]$Path) {
    <# 
    检查指定文件夹是否存在，若不存在则递归创建之
    #>
    if(!(Is-Exist -Path $Path)){
        New-Item -ItemType Directory -Path $Path -Force
        return $true
    }
    return $false
}


function Get-Modifytime([string]$Path) {
    <#
    返回文件或者目录上次修改的时间
    #>
    return (Get-Item $Path).LastWriteTime.ToString().Split(" ")[0].Replace("/","-")
}


function Foulder-To-Zip([string]$Source,[string]$DestDir,[string]$Title) {
    <#
      压缩文件夹，如果成功则返回真
    #>
    Check-Folder -Path $DestDir
    if(Is-Exist -Path $Source){
        $basename = [Path]::basename($Source)
        $ArchiveName = [Path]::join($DestDir,'['+$Title+']'+(Get-Date -Format 'yyyy-MM-dd-HH-mm-ss')+ $basename +".zip")
        Compress-Archive -Path $Source -DestinationPath $ArchiveName -CompressionLevel Optimal
        if($?){
            return $true
        }else{
            $msg = "Some error happens when archive folder`""+$Source+"`"."
            write-host -ForegroundColor 'Red' $msg
        }
    }else{
        $msg = "Source folder `""+$Source+"`" does not exist."
        write-host -ForegroundColor 'Red' $msg
    }
    
    return $false
}

function File-To-Zip([string]$Source,[string]$DestDir,[string]$Title) {
    <#
      压缩文件，如果成功则返回真
    #>
    Check-Folder -Path $DestDir
    if(Is-Exist -Path $Source){
        $basename = [Path]::basename($Source)
        $ArchiveName = [Path]::join($DestDir,'['+$Title+']'+(Get-Date -Format 'yyyy-MM-dd-HH:mm:ss')+ $basename +".zip")
        Compress-Archive -Path $Source -DestinationPath $ArchiveName -CompressionLevel Optimal
        if($?){
            return $true
        }else{
            $msg = "Some error happens when archive file`""+$Source+"`"."
            write-host -ForegroundColor 'Red' $msg
        }
    }else{
        $msg = "Source file `""+$Source+"`" does not exist."
        write-host -ForegroundColor 'Red' $msg
    }
    return $false
}


function Zip-Item([string]$Source,[string]$DestDir,[string]$Title) {
    <#
      压缩文件或者文件夹，如果成功则返回真
    #>
    Check-Folder -Path $DestDir
    if(Is-Exist -Path $Source){
        $basename = [Path]::basename($Source)
        $ArchiveName = [Path]::join($DestDir,'['+$Title+']'+(Get-Date -Format 'yyyy-MM-dd-HH:mm:ss')+ $basename +".zip")
        Compress-Archive -Path $Source -DestinationPath $ArchiveName -CompressionLevel Optimal
        if($?){
            return $true
        }else{
            $msg = "Some error happens when archive `""+$Source+"`"."
            write-host -ForegroundColor 'Red' $msg
        }
    }else{
        $msg = "Source item `""+$Source+"`" does not exist."
        write-host -ForegroundColor 'Red' $msg
    }
    return $false
}

function Zip-Items([string[]]$Sources,[string]$DestDir,[string]$Title) {
    
    Check-Folder -Path $DestDir
    
    $ArchiveName = [Path]::join($DestDir,'['+$Title+']'+" archive at "+(Get-Date -Format 'yyyy-MM-dd-HH-mm-ss')+".zip")
    Compress-Archive -Path $Sources -DestinationPath $ArchiveName -CompressionLevel Optimal
    if($?){
        return $true
    }else{
        $msg = "Some error happens when archive `""+$Source+"`"."
        write-host -ForegroundColor 'Red' $msg
    }
    
}